﻿var count = 0;
var index = -1; //延时一秒以便于DataRefresh()取到数据
var table = new Array();
var Area = new Array();

function DataRefresh() {

	$("#time").html(utils.GetDateTime("yyyy-MM-dd HH:mm:ss"));
	if(count == 0 || count % 60 == 0) {
		api.GetFeedChecked(function(result) {
			if(result.Code == 0) {
				table = JSON.parse(result.Data).Data;
				Area = JSON.parse(result.Data).Areas
			}
		});
	}
	count++;
}

function Tablefresh() {
	var html;
	if(index == 0) {
		var id = Area[0];
		applyHtml(id);
	} else if(index == 10) {
		var id = Area[1];
		applyHtml(id);
	} else if(index == 20) {
		var id = Area[2];
		applyHtml(id);
	} else if(index == 30) {
		var id = Area[3];
		applyHtml(id);
	} else if(index == 40) {
		var id = Area[4];
		applyHtml(id);
	} else if(index == 50) {
		var id = Area[6];
		applyHtml(id);
		index = 0;
	}
	index++
}

function applyHtml(areaId) {
	html = addTd(getTable(table, areaId));
	$("table#feedList tbody").html("");
	$("table#feedList tbody").append(html);
}

function getTable(table, areaId) {
	var areaTable = new Array();
	for(var i = 0; i < table.length; i++) {
		var item = table[i];
		if(item.Area == areaId) {
			areaTable.push(item);
		}
	}
	return areaTable;
}

function addTd(table) {
	var html = "";
	for(var i = 0; i < table.length; i++) {
		var item = table[i];
		html += "<tr><td>" + item.MacName + "</td>";
		html += "<td>" + item.ProdSpec + "</td>";
		html += "<td>" + item.ShiftAmount + "</td>";
		html += "<td>" + item.Binded + "</td>";
		html += "<td>" + item.Oee + "</td>";
		html += turn(item.Tm);
		html += turn(item.Tc);
		html += turn(item.Dj);
		html += turn(item.Nc);
		html += turn(item.Tq);
		html += turn(item.Tt);
		html += turn(item.Gb);
		html += turn(item.Ds0);
		html += turn(item.Ds1);
		html += turn(item.Ds2);
		html += turn(item.Ds3);
		html += turn(item.Ds4);
		html += turn(item.Nb) + "</tr>";
	}
	return html;
}

function turn(value) {
	var mustFeed = '应为:' + value.Data.SMNO + '&#13;';
	var actFeed = '实为:' + value.Data.DMNO + '&#13;';
	var toolsNo = '工装编号:' + value.Data.TOOLS + '&#13;';
	var turncardNo = '周转卡编号:' + value.Data.TNO + '&#13;';
	var feedTiime = '上料时间:' + value.Data.CTIME + '&#13;';
	var tip = mustFeed + actFeed + toolsNo + turncardNo + feedTiime;
	var html;
	if(value.Flag == 0) {
		html = "<td align='center' data-toggle='tooltip' data-html='true'' title='" + tip + "'><a href=''><span class='glyphicon glyphicon-ok'style='color:#00ff00'></span></a></td>"
	} else if(value.Flag == 1) {
		html = "<td align='center' data-toggle='tooltip' data-html='true'' title='" + tip + "'><a href='#'><span class='glyphicon glyphicon-remove'style='color:red'></span></a></td> "
	} else if(value.Flag == 2) {
		html = "<td align='center' data-toggle='tooltip' data-html='true'' title='" + tip + "'><a href='#'><span class='glyphicon glyphicon-minus'></span></a></td>"
	}
	return html;
}
window.onload = function() {
	document.getElementById("ablcok").onclick = function() {
		applyHtml("124");
		clearInterval(tableTimer);
	}
	document.getElementById("bblcok").onclick = function() {
		applyHtml("125");
		clearInterval(tableTimer);
	}
	document.getElementById("dblcok").onclick = function() {
		applyHtml("127");
		clearInterval(tableTimer);
	}
	document.getElementById("eblcok").onclick = function() {
		applyHtml("128");
		clearInterval(tableTimer);
	}
	document.getElementById("gblcok").onclick = function() {
		applyHtml("130");
		clearInterval(tableTimer);
	}
	document.getElementById("hblcok").onclick = function() {
		applyHtml("131");
		clearInterval(tableTimer);
	}
	document.getElementById("default").onclick = function() {
		 window.location.reload();
	}
	var timer = setInterval(function() {
		DataRefresh();
	}, 1000);
	var tableTimer = setInterval(function() {
		Tablefresh();
	}, 1000);
}